$(function(){
    
    
    $.ajaxSetup({
        beforeSend: function(){
            $('#ajax-loading').fadeIn();    
        },
        complete: function(){
            $('#ajax-loading').fadeOut();   
        }
    });
    
    // tooltip
    $('[rel=tooltip]').tooltip();

    // popover
    $('[rel=popover]').popover({
        html : true,        
        content: function() {
            return $('#'+$(this).attr('id')+'-popover').html();
        },
    });

 
    var $window = $(window);
    setTimeout(function () {
        $('.my-sidebar').affix({
            offset: {
                top: function () { return $window.width() <= 980 ? 198 : 118 }, 
                bottom: 270
            }
        });
        
        $('.my-top').affix();
    }, 100)


    // hehe image manage 
    $('a.fixajax').live('click', function(e){
        var popup; 
        var width = 880, height = 520;
        var selector = $(this);
        e.preventDefault();
        $.post($(this).attr('href'), function(data){
            $('#ajax-loading').fadeOut();    
            if(data.error == 1){
                if (popup !== undefined)
                    popup.close();

                var centerWidth = ($(window).width() - width) / 2;
                var centerHeight = ($(window).height() - height) / 2;
                
                popup = window.open(data.url, "openid_popup_window", "width=" + width + ",height=" + height + ",left=" + centerWidth + ",top=" + centerHeight + ",resizable=yes,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no,status=yes");
                popup.focus();
                
                return false;
 
            }
        }, 'json'); 
    });
    
    // format number
    $('.number-format').each(function() {
        var number = parseInt(($(this).text() + '').replace(/[^\d]/g, '')); 
        number = number_format(number, 0, '', '.');
        $(this).text(number);
    });

    $("input.numeric").numeric();
    $("input.numeric.format").bind('keypress keyup paste', function(e){
        var val = parseInt(($(this).val() + '').replace(/[^\d]/g, ''));
        var valFormat = number_format(val, 0, '.', '.');
        $(this).val(valFormat);
    });
})


function scrollToAnchor(selector){
    $('html,body').animate({scrollTop: $(selector).offset().top},'fast');
}